﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using PPI.Data.Inspection;
using PPI.Entities;
using PPI.Entities.Inspections;
using PPI.Entities.Interface;
using PPI.Entities.Utility;
using PPI.Mobile;
using VMind.Global;

namespace PPI.ServiceLogic.Inspections
{
    public class LInspectionStructure : IMobileTable
    {
        public static DataSet SelectAll(string connectionString)
        {
            return DInspectionStructure.SelectAll(connectionString);
        }

        private static List<EInspectionStructure> ParseDatatableInspectionStructure(DataTable dtInspectionStructure)
        {
            List<EInspectionStructure> listInspectionStructure = new List<EInspectionStructure>();

            foreach (DataRow dr in dtInspectionStructure.Rows)
            {
                EInspectionStructure tmpInspectionStructure = new EInspectionStructure();
                tmpInspectionStructure.Id = ConvertHelper.ToInt32(dr["Vendor_InspVisit_XMissionStructureId"]);
                tmpInspectionStructure.ServerID = tmpInspectionStructure.Id;
                tmpInspectionStructure.UtilityAsset = new EUtility_Asset_XMissionStructure(ConvertHelper.ToInt32(dr["Utility_Asset_XMissionStructureFK"]));
                tmpInspectionStructure.Contact = new EVendor_Contact(ConvertHelper.ToInt32(dr["Vendor_ContactFK"]));
                tmpInspectionStructure.VisitType = new EVendor_Visit_Type(ConvertHelper.ToInt32(dr["Vendor_Visit_TypeFK"]));
                tmpInspectionStructure.Project = new EVendor_Project(ConvertHelper.ToInt32(dr["Vendor_ProjectFK"]));
                tmpInspectionStructure.Toptype = new EXMissionStructures_TopType(ConvertHelper.ToInt32(dr["XMissionStructures_TopTypeFk"]));
                tmpInspectionStructure.Latitude = ConvertHelper.Todouble(dr["Latitude"]);
                tmpInspectionStructure.Longitude = ConvertHelper.Todouble(dr["Longitude"]);
                tmpInspectionStructure.Altitude = ConvertHelper.Todouble(dr["Altitude"]);
                tmpInspectionStructure.SatelliteCount = ConvertHelper.ToInt32(dr["SatelliteCount"]);
                tmpInspectionStructure.SatelliteTime = ConvertHelper.ToDateTime(dr["SatelliteTime"]);
                tmpInspectionStructure.SolutionAvailable = ConvertHelper.Tobool(dr["SolutionAvailable"]);
                tmpInspectionStructure.Date = ConvertHelper.ToDateTime(dr["Date"]);
                listInspectionStructure.Add(tmpInspectionStructure);
            }
            return listInspectionStructure;
        }

        public List<EMobileQuery> CreateInsertMobileQuery(string connectionKey)
        {
            List<EMobileQuery> listQueries = new List<EMobileQuery>();

            EMobileQuery _MobileStructures = new EMobileQuery();
            StringBuilder _QueryStructures = new StringBuilder();
            List<SqlParameter> _ParametersStructures = null;

            #region QueryInspectionStructure

            _QueryStructures.AppendLine("Insert into tb_Vendor_InspVisit_XMissionStructures ");
            _QueryStructures.AppendLine(" (");
            _QueryStructures.AppendLine("     Vendor_InspVisit_XMissionStructureId,");
            _QueryStructures.AppendLine("     Vendor_ProjectFk,");
            _QueryStructures.AppendLine("     Utility_Asset_XMisssionStructureFk,");
            _QueryStructures.AppendLine("     Vendor_Visit_TypeFk,");
            _QueryStructures.AppendLine("     XMissionStructures_TopTypeFk,");
            _QueryStructures.AppendLine("     Latitude,");
            _QueryStructures.AppendLine("     Longitude,");
            _QueryStructures.AppendLine("     Altitude,");
            _QueryStructures.AppendLine("     SatelliteCount,");
            _QueryStructures.AppendLine("     SatelliteTime,");
            _QueryStructures.AppendLine("     SolutionAvailable,");
            _QueryStructures.AppendLine("     Vendor_ContactFk,");
            _QueryStructures.AppendLine("     Date");
            _QueryStructures.AppendLine(" )Values(");
            _QueryStructures.AppendLine("     @Vendor_InspVisit_XMissionStructureId,");
            _QueryStructures.AppendLine("     @Vendor_ProjectFK,");
            _QueryStructures.AppendLine("     @Utility_Asset_XMisssionStructureFk,");
            _QueryStructures.AppendLine("     @Vendor_Visit_TypeFK,");
            _QueryStructures.AppendLine("     @XMissionStructures_TopTypeFk,");
            _QueryStructures.AppendLine("     @Latitude,");
            _QueryStructures.AppendLine("     @Longitude,");
            _QueryStructures.AppendLine("     @Altitude,");
            _QueryStructures.AppendLine("     @SatelliteCount,");
            _QueryStructures.AppendLine("     @SatelliteTime,");
            _QueryStructures.AppendLine("     @SolutionAvailable,");
            _QueryStructures.AppendLine("     @Vendor_ContactFK,");
            _QueryStructures.AppendLine("     @Date");
            _QueryStructures.AppendLine(" )");
            _MobileStructures.Query = _QueryStructures.ToString();


            #endregion


            DataTable dtInspectionStructure = SelectAll(connectionKey).Tables[0];
            List<EInspectionStructure> listInspectionStructure =ParseDatatableInspectionStructure(dtInspectionStructure);

            foreach (EInspectionStructure structure in listInspectionStructure)
            {
                _ParametersStructures = new List<SqlParameter>();

                #region ParametersInspectionStructure
                _ParametersStructures.Add(DBHelper.CreateIntParameter("@Vendor_InspVisit_XMissionStructureId", ParameterDirection.Input, false, structure.Id));
                _ParametersStructures.Add(DBHelper.CreateIntParameter("@SolutionAvailable", ParameterDirection.Input, false, structure.SolutionAvailable));

                #region Project
                if (structure.Project.Id != Int32.MinValue)
                    _ParametersStructures.Add(DBHelper.CreateIntParameter("@Vendor_ProjectFK", ParameterDirection.Input, false, structure.Project.Id));
                else
                    _ParametersStructures.Add(DBHelper.CreateIntParameter("@Vendor_ProjectFK", ParameterDirection.Input, true, DBNull.Value));
                #endregion

                #region VisitType
                if (structure.VisitType.Vendor_Visit_TypeId != Int32.MinValue)
                    _ParametersStructures.Add(DBHelper.CreateIntParameter("@Vendor_Visit_TypeFK", ParameterDirection.Input, false, structure.VisitType.Vendor_Visit_TypeId));
                else
                    _ParametersStructures.Add(DBHelper.CreateIntParameter("@Vendor_Visit_TypeFK", ParameterDirection.Input, true, DBNull.Value));
                #endregion

                #region Toptype
                if (structure.Toptype.XMissionStructures_TopTypeId != Int32.MinValue)
                    _ParametersStructures.Add(DBHelper.CreateIntParameter("@XMissionStructures_TopTypeFk", ParameterDirection.Input, false, structure.Toptype.XMissionStructures_TopTypeId));
                else
                    _ParametersStructures.Add(DBHelper.CreateIntParameter("@XMissionStructures_TopTypeFk", ParameterDirection.Input, true, DBNull.Value));
                #endregion

                #region Latitude
                if (structure.Latitude != Double.MinValue)
                    _ParametersStructures.Add(DBHelper.CreateFloatParameter("@Latitude", ParameterDirection.Input, false, structure.Latitude));
                else
                    _ParametersStructures.Add(DBHelper.CreateFloatParameter("@Latitude", ParameterDirection.Input, true, DBNull.Value));
                #endregion

                #region Longitude
                if (structure.Longitude != Double.MinValue)
                    _ParametersStructures.Add(DBHelper.CreateFloatParameter("@Longitude", ParameterDirection.Input, false, structure.Longitude));
                else
                    _ParametersStructures.Add(DBHelper.CreateFloatParameter("@Longitude", ParameterDirection.Input, true, DBNull.Value));
                #endregion

                #region Altitude
                if (structure.Altitude != Double.MinValue)
                    _ParametersStructures.Add(DBHelper.CreateFloatParameter("@Altitude", ParameterDirection.Input, false, structure.Altitude));
                else
                    _ParametersStructures.Add(DBHelper.CreateFloatParameter("@Altitude", ParameterDirection.Input, true, DBNull.Value));
                #endregion

                #region SatelliteCount
                if (structure.SatelliteCount != Int32.MinValue)
                    _ParametersStructures.Add(DBHelper.CreateIntParameter("@SatelliteCount", ParameterDirection.Input, false, structure.SatelliteCount));
                else
                    _ParametersStructures.Add(DBHelper.CreateIntParameter("@SatelliteCount", ParameterDirection.Input, true, DBNull.Value));
                #endregion

                #region SatelliteTime
                if (structure.SatelliteTime != DateTime.MinValue)
                    _ParametersStructures.Add(DBHelper.CreateDateTimeParameter("@SatelliteTime", ParameterDirection.Input, false, structure.SatelliteTime));
                else
                    _ParametersStructures.Add(DBHelper.CreateDateTimeParameter("@SatelliteTime", ParameterDirection.Input, true, DBNull.Value));
                #endregion

                #region Date
                if (structure.Date != DateTime.MinValue)
                    _ParametersStructures.Add(DBHelper.CreateDateTimeParameter("@Date", ParameterDirection.Input, false, structure.Date));
                else
                    _ParametersStructures.Add(DBHelper.CreateDateTimeParameter("@Date", ParameterDirection.Input, true, DBNull.Value));
                #endregion

                #region Contact
                if (structure.Contact.Id != Int32.MinValue)
                    _ParametersStructures.Add(DBHelper.CreateIntParameter("@Vendor_ContactFK", ParameterDirection.Input, false, structure.Contact.Id));
                else
                    _ParametersStructures.Add(DBHelper.CreateIntParameter("@Vendor_ContactFK", ParameterDirection.Input, true, DBNull.Value));
                #endregion

                #region Utility
                if (structure.UtilityAsset.Id != Int32.MinValue)
                    _ParametersStructures.Add(DBHelper.CreateIntParameter("@Utility_Asset_XMisssionStructureFk", ParameterDirection.Input, false, structure.UtilityAsset.Id));
                else
                    _ParametersStructures.Add(DBHelper.CreateIntParameter("@Utility_Asset_XMisssionStructureFk", ParameterDirection.Input, true, DBNull.Value));
                #endregion





                
                #endregion

                _MobileStructures.Parameters.Add(_ParametersStructures);

            }
            listQueries.Add(_MobileStructures);

            return listQueries;
        }

        public static void Delete(Int32 structureId, string connectionKey)
        {
            DInspectionStructure.Delete(structureId,connectionKey);
        }

        public static void DeleteWithPoles(Int32 structureId, string connectionKey)
        {
            DInspectionStructure.DeleteWithPoles(structureId,connectionKey);
        }
    }
}
